﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AndroidRTLPatcher
{
    public partial class frmProtools : Form
    {
        public frmProtools()
        {
            InitializeComponent();
        }

        frmMain mainFrm = new frmMain();
        private void btnPullFiles_Click(object sender, EventArgs e)
        {
            try
            {
                mainFrm.writeToLog("PROTOOLS: Pulling Framework.jar Manually");
                SaveFileDialog saveFramework = new SaveFileDialog();
                saveFramework.OverwritePrompt = true;
                saveFramework.Title = "Select where to save framework.jar";
                saveFramework.FileName = "framework.jar";
                saveFramework.ShowDialog();
                if (saveFramework.FileName == "" || saveFramework.FileName == null)
                {
                    mainFrm.writeToLog("Operation aborted!");
                    MessageBox.Show("Operation aborted!", "Operation Aboreted", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                mainFrm.updateStatus("Fetching framework file...", false, 0, 100);
                mainFrm.writeToLog("Pulling framework file");
                string pullString = mainFrm.ADBShell(@"pull /system/framework/framework.jar """ + saveFramework.FileName.Replace(@"\", @"/"));

                //Final step - check that the file was extracted properly.
                if (!pullString.Contains("KB/s")) //Did not transfer!
                {
                    throw new Exception("File transfer failed!",new Exception(pullString));
                }
                else
                {
                    DialogResult newMsg = MessageBox.Show("File transfered successfully! Do you wish to view it in it's folder?", "File Transfered Successfully!", MessageBoxButtons.YesNo);
                    if (newMsg == System.Windows.Forms.DialogResult.Yes)
                    {
                        string filePath = saveFramework.FileName;
                        if (!File.Exists(filePath))
                        {
                            throw new Exception("The file was not found!");
                        }
                        string argument = @"/select, " + filePath;
                        System.Diagnostics.Process.Start("explorer.exe", argument);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message.Contains("device not found"))
                {
                    MessageBox.Show("No device detected! Make sure your device is in debugging mode, and that ADB drivers are installed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    throw ex;
                }
            }
        }
    }
}
